<template>
  <view class="content">
    <!-- 顶部导航 -->
    <Navbar></Navbar>
    <!-- 
        scroll-view 滚动组件
        scroll-y 开启竖直方向滚动
        scroll-x 开启水平方向滚动
        @scrolltolower 滚动到底部的事件
        refresher-enabled 开启下拉刷新
        @refresherrefresh 下拉刷新事件
        refresher-triggered 下拉刷新的状态控制
    -->
    <scroll-view
      scroll-y
      refresher-enabled
      :refresher-triggered="refresherTriggered"
      @scrolltolower="scrolltolower"
      @refresherrefresh="refresherrefresh"
      class="main"
    >
      <!-- 轮播图 -->
      <Carousel :banners="banners" height="280rpx"></Carousel>
      <!-- 分类 -->
      <CateScroll :categoryHeadMutli="categoryHeadMutli"></CateScroll>
      <!-- 人气推荐 -->
      <view class="panel recommend">
        <view class="item" v-for="item in hotMutli" :key="item.id">
          <view class="title">
            {{ item.title }}<text>{{ item.alt }}</text>
          </view>
          <navigator
            hover-class="none"
            :url="`/pages/recommend/index?type=${item.type}`"
            class="cards"
          >
            <image
              mode="aspectFit"
              v-for="img in item.pictures"
              :key="img"
              :src="img"
            ></image>
          </navigator>
        </view>
      </view>
      <!-- 新鲜好物 -->
      <view class="panel fresh">
        <view class="title">
          新鲜好物
          <navigator
            hover-class="none"
            class="more"
            url="/pages/recommend/index?type=5"
            >更多</navigator
          >
        </view>
        <view class="cards">
          <navigator
            hover-class="none"
            :url="`/pages/goods/index?id=${item.id}`"
            v-for="item in homeNew"
            :key="item.id"
          >
            <image mode="aspectFit" :src="item.picture"></image>
            <view class="name">{{ item.name }}</view>
            <view class="price">
              <text class="small">¥</text>{{ item.price }}
            </view>
          </navigator>
        </view>
      </view>
      <!-- 热门品牌，纯静态，无接口 -->
      <view class="panel brands">
        <view class="title">
          热门品牌
          <navigator hover-class="none" class="more" url="/pages/list/index"
            >更多</navigator
          >
        </view>
        <view class="cards">
          <navigator hover-class="none" url="/pages/goods/index">
            <image
              mode="aspectFit"
              src="http://static.botue.com/erabbit/static/uploads/brand_logo_1.jpg"
            ></image>
            <view class="name">小米</view>
            <view class="price">99元起</view>
          </navigator>
          <navigator hover-class="none" url="/pages/goods/index">
            <image
              mode="aspectFit"
              src="http://static.botue.com/erabbit/static/uploads/brand_logo_2.jpg"
            ></image>
            <view class="name">TCL</view>
            <view class="price">199起</view>
          </navigator>
          <navigator hover-class="none" url="/pages/goods/index">
            <image
              mode="aspectFit"
              src="http://static.botue.com/erabbit/static/uploads/brand_logo_3.jpg"
            ></image>
            <view class="name">饭小宝</view>
            <view class="price">9.9起</view>
          </navigator>
          <navigator hover-class="none" url="/pages/goods/index">
            <image
              mode="aspectFit"
              src="http://static.botue.com/erabbit/static/uploads/brand_logo_4.jpg"
            ></image>
            <view class="name">鳄鱼</view>
            <view class="price">299起</view>
          </navigator>
        </view>
      </view>

      <!-- 猜你喜欢 -->
      <Guess
        :homeGoodsGuessLike="homeGoodsGuessLike"
        :homeGoodsGuessLikeTotal="homeGoodsGuessLikeTotal"
      ></Guess>
    </scroll-view>
  </view>
</template>

<script>
/*
    局部组件，需要正常的引进 - 注册 - 使用
*/
import {
  getHomeBanner,
  getCategoryMutli,
  getHotMutli,
  getHomeNew,
  getGoodsGuessLike,
} from '@/api/home';
import Navbar from './components/Navbar.vue';
export default {
  components: { Navbar },
  data() {
    return {
      banners: [],
      categoryHeadMutli: [],
      //   人气推荐
      hotMutli: [],
      //   新鲜好物
      homeNew: [],
      //   猜你喜欢
      homeGoodsGuessLike: [],
      //   猜你喜欢总数量
      homeGoodsGuessLikeTotal: 0,
      //   下拉刷新状态控制
      refresherTriggered: false,
      /*
       ✨✨✨不建议把分页的数据写在 data 中
        vue中，页面需要用到的数据，一定要写在 data 中，才会是响应式的变化
        如果没有再 data 定义的话，数据变化，不会引起页面的改变

        所以：
        页面需要用到的数据，就写在 data 中
        页面不需要用到的数据，为了性能的优化，不要写在data中
      */
    };
  },
  methods: {
    async getHomeBanner() {
      const res = await getHomeBanner();
      //   console.log('res -----> ', res);
      this.banners = res.result;
    },
    async getCategoryMutli() {
      const res = await getCategoryMutli();
      this.categoryHeadMutli = res.result;
    },
    async getHotMutli() {
      const res = await getHotMutli();
      this.hotMutli = res.result;
    },
    async getHomeNew() {
      const res = await getHomeNew();
      this.homeNew = res.result;
    },
    // 猜你喜欢-有分页数据，本章重点
    async getGoodsGuessLike() {
      const res = await getGoodsGuessLike(this.pageParams);
      //   this.homeGoodsGuessLike = res.result.items;
      //   ✨✨新数据追加到旧数据
      this.homeGoodsGuessLike = [
        ...this.homeGoodsGuessLike,
        ...res.result.items,
      ];
      //   保留后台返回的总页数
      this.pages = res.result.pages;
      //   保留后台返回的总数量
      this.homeGoodsGuessLikeTotal = res.result.counts;
    },
    /*
        0. 查找api，绑定滚动到底部的事件 - @scrolltolower
        1. 判断还有没有下一页， 当前页数 < 后台告知的总页数
        2. this.guessParams.page++,发送请求，新数据追加到旧数据 */
    async scrolltolower(e) {
      /* 
        优化- 添加节流
        1. 触底时，检测是否有请求在发送中 - 可以通过 loading 控制
        2. 有的话，就不要再发
        3. 没有的话，设置 loading 为true，发送请求，请求回来，设置为false
        */
      if (this.loading) return;
      //   console.log('滚动到底部 -----> ', e);
      if (this.pageParams.page < this.pages) {
        this.pageParams.page++;
        this.loading = true;
        await this.getGoodsGuessLike();
        this.loading = false;
      }
    },

    async refresherrefresh(e) {
      /*
            下拉刷新
            1. 查阅api，开启允许下拉刷新refresher-enabled和绑定下拉刷新事件refresherrefresh
            2. 数据处理
                2.1 清空旧数据 - data中的数据，页码数据
                2.2 发送请求，拿回新数据
        */
      //   data数据
      this.banners = [];
      this.categoryHeadMutli = [];
      this.hotMutli = [];
      this.homeNew = [];
      this.homeGoodsGuessLike = [];
      this.homeGoodsGuessLikeTotal = 0;

      //   组件的数据
      this.pages = 1;
      this.loading = false;
      this.pageParams = { page: 1, pageSize: 20 };

      this.refresherTriggered = true;
      //   setTimeout(() => {
      //     this.refresherTriggered = false;
      //   }, 1000);
      //   发送请求
      console.time();
      //   await this.getHomeBanner();
      //   await this.getCategoryMutli();
      //   await this.getHotMutli();
      //   await this.getHomeNew();
      //   await this.getGoodsGuessLike();
      Promise.all([
        this.getHomeBanner(),
        this.getCategoryMutli(),
        this.getHotMutli(),
        this.getHomeNew(),
        this.getGoodsGuessLike(),
      ]).then(() => {
        console.timeEnd();
        this.refresherTriggered = false;
      });
    },
  },
  onLoad(options) {
    // 进入页面时，赋值初始数据
    this.pages = 1;
    this.loading = false;
    this.pageParams = { page: 1, pageSize: 20 };
    this.getHomeBanner();
    this.getCategoryMutli();
    this.getHotMutli();
    this.getHomeNew();
    this.getGoodsGuessLike();
  },
};
</script>

<style lang="scss" scoped>
.content {
  height: 100vh;
  display: flex;
  flex-direction: column;
}
.main {
  flex: 1;
  overflow: hidden;
  background-color: #f7f7f8;
}
/* 公共面板 */
.panel {
  margin: 20rpx 20rpx 0;
  border-radius: 10rpx;
  background-color: #fff;
  .title {
    display: flex;
    align-items: center;
    padding: 24rpx 24rpx 0;
    font-size: 32rpx;
    color: #262626;
    position: relative;
    text {
      font-size: 24rpx;
      color: #7f7f7f;
      margin-left: 18rpx;
    }
  }
  .more {
    position: absolute;
    right: 24rpx;
    font-size: 26rpx;
    color: #7f7f7f;
  }
  .cards {
    display: flex;
  }
  .name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
/* 推荐专区 */
.recommend {
  display: flex;
  flex-wrap: wrap;
  .item {
    display: flex;
    flex-direction: column;
    width: 50%;
    height: 254rpx;
    border-right: 1rpx solid #eee;
    border-top: 1rpx solid #eee;
    &:nth-child(2n) {
      border-right: 0 none;
    }
    &:nth-child(-n + 2) {
      border-top: 0 none;
    }
  }
  .cards {
    flex: 1;
    padding: 15rpx 20rpx;
    justify-content: space-between;
    image {
      width: 48%;
    }
  }
}

/* 新鲜好物 */
.fresh {
  min-height: 320rpx;
  .price {
    line-height: 1;
    text-align: left;
    font-size: 26rpx;
    color: #cf4444;
  }
  .small {
    font-size: 80%;
  }
}
.fresh .cards navigator,
.brands .cards navigator {
  width: 25%;
  padding: 15rpx 20rpx 20rpx;
  line-height: 1;
  text-align: center;
}
.fresh image,
.brands image {
  width: 126rpx;
  height: 126rpx;
}
.fresh .name,
.brands .name {
  margin: 20rpx 0 16rpx;
  font-size: 24rpx;
  color: #262626;
}
.brands {
  .price {
    font-size: 24rpx;
    color: #999;
  }
}
</style>
